﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;

namespace DAL
{
    // Token: 0x02000008 RID: 8
    public class ProblemInfoDAL
    {
        // Token: 0x040000A0 RID: 160
        public static readonly string SqlConnString = ConfigurationManager.ConnectionStrings["ELCO_ConnectionString"].ConnectionString;
        // Token: 0x060000F3 RID: 243 RVA: 0x000127BC File Offset: 0x000109BC
        public static DataTable GetProblemInfo(string sortname, string sortorder, string ToUserNo, string keyvalue)
        {
            DataTable dataTable = new DataTable();
            DataTable result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlConnection.Open();
                sqlCommand.Connection = sqlConnection;
                string commandText = string.Concat(new string[]
				{
					"select b.ID,b.MessageTime,b.FormUserNo,c.UserName FormUserName,b.MessageWords,a.ProblemImage,a.ProblemVideo,b.RepeateState from ProblemInfo a left join ProblemMessage b on a.ID = b.ProblemID left join UserInfo c on b.FormUserNo = c.UserNo  where b.ToUserNo = '",
					ToUserNo,
					"' and c.UserName  like '%",
					keyvalue,
					"%' order by b.MessageTime desc"
				});
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = commandText;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                sqlDataAdapter.Fill(dataTable);
                result = dataTable;
            }
            return result;
        }

        public static DataTable GetProblemInfoSend(string sortname, string sortorder, string ToUserNo, string keyvalue)
        {
            DataTable dataTable = new DataTable();
            DataTable result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlConnection.Open();
                sqlCommand.Connection = sqlConnection;
                string commandText = string.Concat(new string[]
				{
					"select b.ID,b.MessageTime,c.UserName ToUserNo,b.MessageWords,a.ProblemImage,a.ProblemVideo,b.RepeateState from ProblemInfo a left join ProblemMessage b on a.ID = b.ProblemID left join UserInfo c on b.ToUserNo = c.UserNo  where b.FormUserNo = '",
					ToUserNo,
					"'  order by b.MessageTime desc"
				});
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = commandText;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                sqlDataAdapter.Fill(dataTable);
                result = dataTable;
            }
            return result;
        }

        // Token: 0x060000F4 RID: 244 RVA: 0x00012878 File Offset: 0x00010A78
        public static string saveProblemInfo(string CustomerName, string FromUserNo, string ToUserNo, string ProblemIssue, string ProblemImage, string VideoPath)
        {
            string result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlTransaction sqlTransaction = null;
                try
                {
                    sqlConnection.Open();
                    sqlTransaction = sqlConnection.BeginTransaction();
                    sqlCommand.Transaction = sqlTransaction;
                    sqlCommand.Connection = sqlConnection;
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] array = new SqlParameter[6];
                    array[0] = new SqlParameter("@CustomerName", CustomerName);
                    array[0].Direction = ParameterDirection.Input;
                    array[1] = new SqlParameter("@FromUserNo", FromUserNo);
                    array[1].Direction = ParameterDirection.Input;
                    array[2] = new SqlParameter("@ToUserNo", ToUserNo);
                    array[2].Direction = ParameterDirection.Input;
                    array[3] = new SqlParameter("@ProblemIssue", ProblemIssue);
                    array[3].Direction = ParameterDirection.Input;
                    array[4] = new SqlParameter("@ProblemImage", ProblemImage);
                    array[4].Direction = ParameterDirection.Input;
                    array[5] = new SqlParameter("@VideoPath", VideoPath);
                    array[5].Direction = ParameterDirection.Input;
                    foreach (SqlParameter value in array)
                    {
                        sqlCommand.Parameters.Add(value);
                    }
                    sqlCommand.CommandText = "[usp_Problem_AddProblemInfo]";
                    sqlCommand.ExecuteNonQuery();
                    sqlTransaction.Commit();
                    result = "success";
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    result = "falut";
                }
            }
            return result;
        }

        // Token: 0x060000F5 RID: 245 RVA: 0x00012A10 File Offset: 0x00010C10
        public static string saveMessageRepate(string MessageID, string MessageWordsRepate, string FromUserNo, string ToUserNo, string ProblemImage, string VideoPath)
        {
            string result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlTransaction sqlTransaction = null;
                try
                {
                    sqlConnection.Open();
                    sqlTransaction = sqlConnection.BeginTransaction();
                    sqlCommand.Transaction = sqlTransaction;
                    sqlCommand.Connection = sqlConnection;
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] array = new SqlParameter[6];
                    array[0] = new SqlParameter("@MessageID", MessageID);
                    array[0].Direction = ParameterDirection.Input;
                    array[1] = new SqlParameter("@MessageWordsRepate", MessageWordsRepate);
                    array[1].Direction = ParameterDirection.Input;
                    array[2] = new SqlParameter("@FromUserNo", FromUserNo);
                    array[2].Direction = ParameterDirection.Input;
                    array[3] = new SqlParameter("@ToUserNo", ToUserNo);
                    array[3].Direction = ParameterDirection.Input;
                    array[4] = new SqlParameter("@ProblemImage", ProblemImage);
                    array[4].Direction = ParameterDirection.Input;
                    array[5] = new SqlParameter("@VideoPath", VideoPath);
                    array[5].Direction = ParameterDirection.Input;
                    foreach (SqlParameter value in array)
                    {
                        sqlCommand.Parameters.Add(value);
                    }
                    sqlCommand.CommandText = "[usp_Problem_AddMessageRepate]";
                    sqlCommand.ExecuteNonQuery();
                    sqlTransaction.Commit();
                    result = "success";
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    result = "falut";
                }
            }
            return result;
        }

        // Token: 0x060000F6 RID: 246 RVA: 0x00012C84 File Offset: 0x00010E84
        public static string getVideoByProblemID(string ProblemID)
        {
            string result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                string sql = "select ProblemVideo from ProblemInfo  where ID=@ID ";
                result = sqlConnection.Query<string>(sql, new
                {
                    ID = ProblemID
                }, null, true, null, null).FirstOrDefault();
            }
            return result;
        }

        // Token: 0x060000F7 RID: 247 RVA: 0x00012CF8 File Offset: 0x00010EF8
        public static string getImgByProblemID(string ProblemID)
        {
            string result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                string sql = "select ProblemImage from ProblemInfo  where ID=(select ProblemID from ProblemMessage where ID=@ID) ";
                result = sqlConnection.Query<string>(sql, new
                {
                    ID = ProblemID
                }, null, true, null, null).FirstOrDefault();
            }
            return result;
        }

        // Token: 0x060000F8 RID: 248 RVA: 0x00012E48 File Offset: 0x00011048
        public static string getMessageInfoByID(string MessageID)
        {
            string result;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                string sql = "select MessageWords from ProblemMessage  where ID=@MessageID ";
                result = sqlConnection.Query<string>(sql, new
                {
                    MessageID
                }, null, true, null, null).FirstOrDefault();
            }
            return result;
        }

        // Token: 0x060000F9 RID: 249 RVA: 0x00012F98 File Offset: 0x00011198
        public static int deleteProblemInfo(string ProblemID)
        {
            int result = 0;
            using (SqlConnection sqlConnection = new SqlConnection(ProblemInfoDAL.SqlConnString))
            {
                string sql = "DELETE FROM  ProblemInfo WHERE ID = @ProblemID;";
                result = sqlConnection.Execute(sql, new
                {
                    ProblemID
                }, null, null, null);
            }
            return result;
        }

        
    }
}
